use std::fmt;

#[derive(Debug, Clone)]
pub enum RecordType {
    Byte(bool, bool), // singed, le, 如果为false（默认）的话是无符号和大端
    Word(bool, bool),
    Long(bool, bool),
    Bcd,
    Hex,
    HexString,
    Block,
    Char(String), // 编码格式
    Date,
    Time,
    Timestamp,
    CustomStruct(String),
    CustomDictionary(String),
    _InnerCustomStruct,     // 用于程序内部
    _InnerCustomDictionary, // 用于程序内部
    None,
}

#[derive(Debug, Clone)]
pub struct Record {
    pub name: String,          // 字段名
    pub date_type: RecordType, // 如果是 struct或者是enum，则record_vec会有值，表示内部含有的内容
    pub length: u32,
    pub description: String,
    pub record_vec: Vec<Record>,

    pub loop_counter_name: String, // loop counter 的名字
    pub loop_counter_define: bool, // =true表示在定义
    pub loop_counter_using: bool,  // =true表示在使用
}

impl Record {
    pub fn new() -> Record {
        let r = Record {
            name: String::new(),
            date_type: RecordType::None,
            length: 0,
            description: String::new(),
            record_vec: Vec::new(),
            loop_counter_name: String::new(),
            loop_counter_define: false,
            loop_counter_using: false,
        };

        return r;
    }
}

// 定义一个自定义错误类型
#[derive(Debug)]
pub enum FmtParseError {
    MyInfo(String),
    MyError(String),
}

// 为自定义错误类型实现 Error trait
impl fmt::Display for FmtParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FmtParseError::MyInfo(msg) => write!(f, "info: {}", msg),
            FmtParseError::MyError(msg) => write!(f, "error: {}", msg),
        }
    }
}
